package com.hewang.helloandroid.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hewang.helloandroid.R;

public class MyListAdapter extends BaseAdapter {
    private Context mContext; // 上下文对象
    private LayoutInflater mLayoutInflater; // 布局xml文件对应的实例化View对象

    public MyListAdapter(Context context) { // 重写构造函数
        this.mContext = context; // 传入上下文
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() { // 显示几行
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolder {
        public ImageView imageView;
        public TextView tvTitle, tvTime, tvContent;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) { // 每个子项被滚动到屏幕内的时候会被调用
        ViewHolder holder = null;
        if(convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.layout_list_item, null); // 为子项加载传入布局
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 给控件赋值
        holder.tvTitle.setText("这是标题");
        holder.tvTime.setText("2088-08-08");
        holder.tvContent.setText("这是内容");
        Glide.with(mContext).load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F24%2F20200424164538_ppmav.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1635911909&t=e3b7eb941540d91081f3359841a7322f").into(holder.imageView);
        return convertView;
    }
}
